package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.workflow.bpmconst.BpmConst;
import com.ccp.dev.workflow.dao.BpmFormRunDao;
import com.ccp.dev.workflow.dao.BpmNodeSetDao;
import com.ccp.dev.workflow.model.BpmFormRun;
import com.ccp.dev.workflow.model.BpmNodeSet;
import com.ccp.dev.workflow.model.FlowNodeInfo;
import com.ccp.dev.workflow.model.NodeCache;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassDescribe:流程表单运行情况 Service类
 * @author :jiayueqiao
 * Date: 2019-07-22
 * Since:1
 */
@Service
public class BpmFormRunService extends BaseService<BpmFormRun> {

    @Resource
    private BpmFormRunDao bpmFormRunDao;
    @Resource
    private BpmNodeSetDao bpmNodeSetDao;


    /**
     * 获取第一个节点的设置，如果没有设置，则获取全局节点设置
     * @param procDefId 流程定义id
     * @return 节点设置
     * @throws Exception 异常
     */
    public BpmNodeSet getStartBpmNodeSet(String procDefId) throws Exception {
        //开始节点后的第一个节点
        FlowNodeInfo flowNodeInfo = NodeCache.getFirstNodeInfo(procDefId);
        String nodeId = "";
        if (flowNodeInfo != null) {
            nodeId = flowNodeInfo.getNodeId();
        }
        Map<String, String> params = new HashMap<>(2);
        params.put("procDefId",procDefId);
        params.put("nodeId",nodeId);
        BpmNodeSet firstNodeSet = bpmNodeSetDao.getByProcDefIdNodeIdOrParentProcDefId(params);
        if (firstNodeSet != null && !BpmConst.FORM_TYPE_NULL.equals(firstNodeSet.getFormType())) {
            return firstNodeSet;
        }
        BpmNodeSet globalNodeSet = bpmNodeSetDao.getGlobalByProcDefId(procDefId);
        if (globalNodeSet != null && !BpmConst.FORM_TYPE_NULL.equals(globalNodeSet.getFormType())) {
            return globalNodeSet;
        }
        return null;

    }

    /**
     * 添加表单运行情况。
     * <pre>
     * 将当前最新的表单配置信息添加到表单运行情况。
     * 之后的流程表单从表单运行情况表中取值。
     * </pre>
     *
     * @param procDefId      表单定义ID
     * @param runId         process runID
     * @param procInstId 流程实例ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void addFormRun(String procDefId, String runId, String procInstId) {
        List<BpmNodeSet> list = bpmNodeSetDao.getOnlineFormByProcDefId(procDefId);
        for (BpmNodeSet bpmNodeSet : list) {
            BpmFormRun bpmFormRun = getByBpmNodeSet(runId, procInstId, bpmNodeSet);
            bpmFormRunDao.add(bpmFormRun);
        }
    }

    /**
     * 添加表单运行情况。
     * <pre>
     * 将当前最新的表单配置信息添加到表单运行情况。
     * 之后的流程表单从表单运行情况表中取值。
     * </pre>
     *
     * @param procDefId      表单定义ID
     * @param runId         process runID
     * @param procInstId 流程实例ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void addFormRun(String procDefId, String runId, String procInstId, String parentProcDefId) {
        List<BpmNodeSet> list = bpmNodeSetDao.getOnlineFormByProcDefIdAndParentProcDefId(procDefId, parentProcDefId);
        for (BpmNodeSet bpmNodeSet : list) {
            BpmFormRun bpmFormRun = getByBpmNodeSet(runId, procInstId, bpmNodeSet);
            bpmFormRunDao.add(bpmFormRun);
        }
    }

    /**
     * 根据BpmNodeSet对象构建BpmFormRun对象
     * @param runId 运行id
     * @param procInstId 流程实例id
     * @param bpmNodeSet 节点设置
     * @return BpmFormRun
     */
    private BpmFormRun getByBpmNodeSet(String runId, String procInstId, BpmNodeSet bpmNodeSet) {
        BpmFormRun bpmFormRun = new BpmFormRun();
        bpmFormRun.setId(UUIDUtils.getUUIDFor32());
        bpmFormRun.setRunId(runId);
        bpmFormRun.setProcInstId(procInstId);
        bpmFormRun.setProcDefId(bpmNodeSet.getProcDefId());
        bpmFormRun.setProcNodeId(bpmNodeSet.getNodeId());
        bpmFormRun.setFormDefId(bpmNodeSet.getFormDefId());
        bpmFormRun.setFormDefKey(bpmNodeSet.getFormKey());
        bpmFormRun.setFormType(bpmNodeSet.getFormType());
        bpmFormRun.setFormUrl(bpmNodeSet.getFormUrl());
        bpmFormRun.setSetType(bpmNodeSet.getSetType());
        return bpmFormRun;
    }

}
